using System;
using System.Collections.Generic;
using System.Text;
using BAFIRCSharp;

namespace BAFIRCSharpTest
{
    class Program
    {
        static void Main(string[] args)
        {
            /***IRCConnection irc = new IRCConnection();
            IdentD ident = new IdentD("ftest", "Woot");
            ident.StartIdentD();
            ServerInfo server = new ServerInfo("irc.us.freenode.net", 6667);
            irc.Connect(server);
            irc.SendMessage(new BAFIRCSharp.Messages.Nick("baftest"));
            irc.SendMessage(new BAFIRCSharp.Messages.User("a", "b", "c", "d"));

            irc.SendMessage(new BAFIRCSharp.Messages.Join("#iia,#silly-joy"));
            irc.SendMessage(new BAFIRCSharp.Messages.PrivMsg("#iia", "Hey!"));
            irc.SendMessage(new BAFIRCSharp.Messages.PrivMsg("#iia", "What's up?"));

            bool Going = true;
            while (Going)
            {
                if (irc.Poll())
                {
                    IRCMessage msg;
                    while ((msg = irc.GetMessage()) != null)
                    {
                        if (msg.Command == "PRIVMSG")
                        {
                            if (CTCPMessage.IsCTCP(msg))
                            {
                                CTCPMessage CTCP = new CTCPMessage(msg);
                                if(CTCP.Type == CTCPType.CTCPQuery)
                                    Console.WriteLine("CTCP " + CTCP.Command + " from " + msg.Sender.Nickname + " (" + CTCP.Params.ToString() + ")");
                                else
                                    Console.WriteLine("CTCP [response] " + CTCP.Command + " from " + msg.Sender.Nickname + " (" + CTCP.Params.ToString() + ")");

                                if (CTCP.Type == CTCPType.CTCPQuery)
                                {
                                    Console.WriteLine(CTCP.Command);
                                    if (CTCP.Command == "VERSION")
                                    {
                                        irc.SendMessage(new BAFIRCSharp.Messages.CTCPReply(msg.Sender.Nickname, "VERSION", "BAFIRC:v0.1:Windows"));
                                    }

                                    if (CTCP.Command == "PING")
                                    {
                                        irc.SendMessage(new BAFIRCSharp.Messages.CTCPReply(msg.Sender.Nickname, "PING", CTCP.Params));
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("<" + msg.Sender.Nickname + " @ " + msg.Params[0] + "> " + msg.Params[1]);
                                if (msg.Params[1].ToLower() == "!test")
                                    irc.SendMessage("PRIVMSG " + msg.Params[0] + " :FUCK YOU " + msg.Sender.Nickname + "\r\n");
                                if (msg.Params[1].Length > 5 && msg.Params[1].Substring(0, 5).ToLower() == "!bcom")
                                {
                                    //if (!msg.Sender.Nickname.ToLower().Contains("baf"))
                                    if (msg.Sender.Hostname.ToLower() != "allegro/botmaster/baf" && msg.Sender.Hostname.ToLower() != "allegro/user/sevalecan")
                                        irc.SendMessage(new BAFIRCSharp.Messages.PrivMsg(msg.Params[0], "You aren't authorized."));
                                    else if (msg.Params[1].Length > 7)
                                        irc.SendMessage(msg.Params[1].Substring(6) + "\r\n");
                                }
                                if (msg.Params[1].Length >= 4 && msg.Params[1].Substring(0, 4).ToLower() == "!die")
                                {
                                    //if (!msg.Sender.Nickname.ToLower().Contains("baf"))
                                    if (msg.Sender.Hostname.ToLower() != "allegro/botmaster/baf")
                                        irc.SendMessage(new BAFIRCSharp.Messages.PrivMsg(msg.Params[0], "You aren't authorized."));
                                    else
                                    {
                                        irc.SendMessage(new BAFIRCSharp.Messages.Quit("!die by " + msg.Sender.Nickname + " (" + msg.Sender.Username + "@" + msg.Sender.Hostname + ")"));
                                        System.Threading.Thread.Sleep(1000);
                                        irc.Disconnect();
                                        Going = false;
                                    }
                                }
                                if (msg.Params[1].Length >= 7 && msg.Params[1].Substring(0, 7).ToLower() == "!flushq")
                                {
                                    Console.WriteLine(msg.Sender.Hostname.ToLower());
                                    //if (!msg.Sender.Nickname.ToLower().Contains("baf"))
                                    if(msg.Sender.Hostname.ToLower() != "allegro/botmaster/baf" && msg.Sender.Hostname.ToLower() != "allegro/user/sevalecan")
                                        irc.SendMessage(new BAFIRCSharp.Messages.PrivMsg(msg.Params[0], "You aren't authorized."));
                                    else
                                    {
                                        irc.FlushQ();
                                        irc.SendMessage(new BAFIRCSharp.Messages.PrivMsg(msg.Params[0], "Send buffer flushed by " + msg.Sender.Nickname));
                                    }
                                }
                            }
                        }
                        else if (msg.Command == "NOTICE")
                        {
                            Console.WriteLine("-" + msg.Sender.Nickname + " @ " + msg.Params[0] + "- " + msg.Params[1]);
                        }
                        else if (msg.Command == "PING")
                        {
                            Console.WriteLine("<<Ping>>");
                            irc.SendMessage(new BAFIRCSharp.Messages.Pong(msg.Params[0]));
                        }
                        else if (msg.Command == "MODE")
                        {
                            //Console.WriteLine(msg.Sender.Nickname + " sets mode " + msg.Params[1] + " " + msg.Params[2] + " on " + msg.Params[0]);
                        }
                        else if (msg.Command == "JOIN")
                        {
                            Console.WriteLine(msg.Sender.Nickname + " joins " + msg.Params[0]);
                        }
                        /**else if (msg.Command == "001" || msg.Command == "002" || msg.Command == "003" || msg.Command == "004" || msg.Command == "005" || msg.Command == "250" || msg.Command == "251" || msg.Command == "252" || msg.Command == "254" || msg.Command == "255" || msg.Command == "265" || msg.Command == "266")
                        {
                            Console.WriteLine(msg.Params[1]);
                        }
                        else if (msg.Command == "372")
                        {
                            Console.WriteLine("MOTD: " + msg.Params[1]);
                        }
                        else if (msg.Command == "375")
                        {
                            Console.WriteLine("START of MOTD");
                        }
                        else if (msg.Command == "376")
                        {
                            Console.WriteLine("END of MOTD");
                        }
                        else if (msg.Command == "332")
                        {
                            Console.WriteLine(msg.Params[1] + " : " + msg.Params[2]);
                        }
                        else if (msg.Command == "353")
                        {
                            Console.WriteLine("Users: " + msg.Params[3]);
                        }** /
                        else
                        {
                            Int32 CommandCode;
                            if (Int32.TryParse(msg.Command, out CommandCode))
                            {
                                Console.WriteLine("Unhandled Numeric [" + (NumericReplies)CommandCode + "]:" + msg.Message);
                                //irc.SendMessage(new BAFIRCSharp.Messages.PrivMsg("#silly-joy", "Unhandled Numeric (" + (NumericReplies)CommandCode + ") - " + msg.Message));
                            }
                            else
                                Console.WriteLine("Unhandled Message: " + msg.Message);
                            /*Console.WriteLine("Command: " + msg.Command + "   Number Params: " + msg.Params.Count);
                            Console.WriteLine("Has Sender Information: " + msg.HasSenderInf);
                            if (msg.HasSenderInf)
                            {
                                Console.WriteLine(" Nick: " + msg.Sender.Nickname);
                                Console.WriteLine(" User: " + msg.Sender.Username);
                                Console.WriteLine(" Host: " + msg.Sender.Hostname);
                            }
                            foreach (string Param in msg.Params)
                            {
                                Console.WriteLine("Param: " + Param);
                            }* /
                        }
                    }
                }
                if (ident.Active && ident.ServedReqs > 0)
                    ident.StopIdentD();

                if (!irc.Connected)
                {
                    ident.ServedReqs = 0;
                    try
                    {
                        ident.StartIdentD();
                    }
                    catch (System.Threading.ThreadStateException tse)
                    {
                        Console.WriteLine(tse.Message);
                    }

                    irc.ReConnect();
                    irc.SendMessage(new BAFIRCSharp.Messages.Nick("baftest"));
                    irc.SendMessage(new BAFIRCSharp.Messages.User("a", "b", "c", "d"));

                    irc.SendMessage(new BAFIRCSharp.Messages.Join("#allegro,#iia,#silly-joy"));
                }

                System.Threading.Thread.Sleep(100);
            }*/

            IRCNetwork IRC = new IRCNetwork();
            IRC.Nicks.Add("baftest");
            IRC.Realname = "BAFIRC Test";
            IRC.Username = "bafirc";
            IRC.Servers.Add(new ServerInfo("irc.us.freenode.net", 6667));
            IRC.EnableIdent = true;
            IRC.Connect();
            IRC.Connection.SendMessage(new BAFIRCSharp.Messages.Join("#iia"));

            while (IRC.Alive)
                System.Threading.Thread.Sleep(1000);

            //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

        }
    }
}
